package org.lee.myshiroredisplus.controller;

import org.lee.myshiroredisplus.entity.User;
import org.lee.myshiroredisplus.service.UserService;
import org.lee.myshiroredisplus.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jackielee
 * @version 1.0
 * @description UserController
 * @date 2021/4/9
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate  redisTemplate;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/test/{id}")
    public Object test(@PathVariable("id") int id){
        User user = userService.getById(id);
        String key1 = "stringRedisTemplate";
        stringRedisTemplate.opsForValue().set(key1, "this is the demo welcome.");
        System.out.println(stringRedisTemplate.opsForValue().get(key1));

        String key3 = "redisTemplate";
        redisTemplate.opsForValue().set(key3, "this is from redisTemplate");
        System.out.println("get by redisTemplate key3:"+redisTemplate.opsForValue().get(key3));
        System.out.println("get by redis util key3:"+redisUtil.get(key3));

        String key2 = "user-lee";
        redisUtil.set(key2, user, 1000);

        Object obj = redisUtil.get(key2);
        User u1 = (User) obj;
        System.out.println(u1.toString());

        return u1;
    }
}
